﻿using UnityEngine;



    public static class VectorExtension {

        public static Vector2 xy (this Vector3 v) {
            return new Vector2(v.x, v.y);
        }

        // orthogonal vector v2 to v1
        public static Vector2 Orth (this Vector2 from, Vector2 to) {
            return from.Project(to) - from;
        }

        // projection vector v2 to v1
        public static Vector2 Project(this Vector2 from, Vector2 to) {
            return to * (Vector2.Dot(to, from) / Mathf.Pow(to.magnitude, 2f));
        }

		public static Vector2 Perp (this Vector2 v) {
			return new Vector2(v.y, -v.x);
		}
    /// <summary>
    /// 得到平面投影向量
    /// </summary>
    /// <param name="point"></param>
    /// <returns></returns>
    public static Vector3 GetPlaneVector(this Vector3 vector3)
    {
        return new Vector3(vector3.x, 0, vector3.z);
    }
    public static Vector3 FromToMoveRotation(this Vector3 vector3, Vector3 location, Vector3 direction)
    {
        return vector3.FromToMoveRotation(location, direction, Vector3.one);
    }
    public static Vector3 FromToMoveRotation(this Vector3 vector3, Vector3 location, Vector3 direction, Vector3 scale)
    {

        Matrix4x4 matrix = Matrix4x4.identity;
        Quaternion quaternion = Quaternion.FromToRotation(Vector3.forward, direction);

        matrix.SetTRS(location, quaternion, scale);
        vector3 = matrix.MultiplyPoint3x4(vector3);

        return vector3;
    }
    /// <summary>
    /// 角度旋转
    /// </summary>
    /// <param name="vector3"></param>
    /// <param name="angle">旋转角度</param>
    /// <param name="center">旋转中心点</param>
    /// <param name="direction">旋转轴</param>
    /// <returns></returns>
    public static Vector3 ToAngle(this Vector3 vector3, float angle, Vector3 center, Vector3 direction)
    {
        Vector3 pos = center;
        Quaternion quaternion = Quaternion.AngleAxis(angle, direction);
        Matrix4x4 matrix = new Matrix4x4();
        matrix.SetTRS(pos, quaternion, Vector3.one);
        vector3 = matrix.MultiplyPoint3x4(vector3);
        return vector3;
    }
}




